<?php

/* 
 * Author: Mario Jose
 * email: mariognu-free@yahoo.com.br
 *
 * created: August, 22nd 2007
 * modified: July, 13rd 2008
*/

/*
  This script is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This script is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/*
 * List files and folders from url. Make arrays and string with paths and
 * names from the same.
*/

class folderDescriptor 
	{
   /*
    * Vars with folders content in url
    *
    * @var string $folder_path With url to folder
    * @var array $folder_listed Array with all folders listed from url
    * @var string $folder_rand With name of every folder listed. Set only if
    *     function folderRant() start
   */
	var $folder_path;
	var $folder_listed;
	var $folder_rand;

   /*
    * Vars with file content in url
    *
    * @var string $file_path With url to file
    * @var array $file_listed Array with all file listed from url
    * @var string $file_rand With name of every file listed. Set only if
    *     function fileRant() start
   */
	var $file_extension;
	var $file_listed;
	var $file_rand;

   /*
    * Var with state of functions runs
    *
    * @var bool $flag
   */	
	var $flag;
   /*
    * Var with objetc opendir
    *
    * @var object $handle
   */	
	var $handler;

   /*
    * Make array with folders and files setting filter passed in parameter
    *
    * @param string $path Path from url to be have file and folder listed
    * @param string|bool $extension Extension to be used with filter
   */		
	function folderDescriptor ($path, $extension = FALSE)
		{
      // Verify if last char is "/"
		if (substr($path, (strlen($path) - 1), strlen($path)) != '/')
			$this->folder_path = $path . '/';
		else
			$this->folder_path = $path;
			
		$this->flag = TRUE;
		$this->file_extension = $extension;
		
		$this->handler = @opendir($this->folder_path);
		
		if (!$this->handler) {
			$this->flag = FALSE;
			return;
			}
		else
			{
			while ($file = readdir($this->handler))
				{
				if (filetype($this->folder_path . $file) == 'file')
					{ // list files
					if ($this->file_extension) 
						{ //file type filter
						$fileExtension = pathinfo ($this->folder_path . $file); //make array with file informations
						if ($fileExtension['extension'] == $this->file_extension) 
							{
							$this->file_listed[] = $file; //make array with files filter
							}
						}
					else
						{ //all file types
						if (filetype ($this->folder_path . $file) == 'file') $this->file_listed[] = $file; // make array without file filter
						}
					}
				elseif (filetype ($this->folder_path . $file) == 'dir' && $file != '.' && $file != '..')
					{ //list folders
					$this->folder_listed[] = $file;
					}					
				} //end while
			if (!empty($this->folder_listed)) sort($this->folder_listed);
         if (!empty($this->file_listed)) sort($this->file_listed);
			} // end else handler verify
		} // end function
	
	function mountBrowser ()
		{
			// Mount browser folders and files
		}
	
	function mountNav ()
		{
			// Mount navigations
		}
		
   /*
    * Select one of file listed randon and set $file_rand
   */
	function fileRand ()
		{
		if (!$this->flag) return FALSE;
		else $this->file_rand = $this->file_listed[(rand (0, (count ($this->file_listed)) - 1))];
		}
   /*
    * Select one of folder listed randon and set $folder_rand
   */		
	function folderRand ()
		{
		if (!$this->flag) return FALSE;
		else $this->folder_rand = $this->folder_listed[(rand (0, (count ($this->folder_listed)) - 1))];
		}			
		
	} //end class

?>
